﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Delay;

namespace lfm.Parser
{
    internal class Album
    {
        public static async Task<lfm.Album> getInfo(String e)
        {
            lfm.Album result = new lfm.Album();

            // Make Method Run Asynchronously
            await Task.Run(() =>
            {
                XDocument x = XDocument.Parse(e);

                // Parse main album details
                result = (from u in x.Descendants("album")
                          select new lfm.Album
                          {
                              Name = u.SafeGetElement("name").SafeGetStringValue(),
                              Artist = new lfm.Artist { Name = u.SafeGetElement("artist").SafeGetStringValue() },
                              ID = u.SafeGetElement("id").SafeGetInt64Value(),
                              ReleaseDate = u.SafeGetElement("releasedate").SafeGetDateTimeValue(DateTime.Now),
                              Listeners = u.SafeGetElement("listeners").SafeGetInt64Value(),
                              Playcount = u.SafeGetElement("playcount").SafeGetInt64Value(),
                              ImageSmall = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "small").FirstOrDefault().SafeGetUriValue(),
                              ImageMedium = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "medium").FirstOrDefault().SafeGetUriValue(),
                              ImageLarge = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "large").FirstOrDefault().SafeGetUriValue(),
                              ImageExtraLarge = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "extralarge").FirstOrDefault().SafeGetUriValue(),
                              ImageMega = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "mega").FirstOrDefault().SafeGetUriValue(),
                              Tracks = new List<lfm.Track>(),
                              TopTags = new List<lfm.Tag>(),
                              URL = u.SafeGetElement("url").SafeGetUriValue(),
                              WikiPublished = u.SafeGetElement("wiki").SafeGetElement("published").SafeGetDateTimeValue(DateTime.Now),
                              WikiSummary = u.SafeGetElement("wiki").SafeGetElement("summary").SafeGetStringValue(),
                              WikiContent = u.SafeGetElement("wiki").SafeGetElement("content").SafeGetStringValue()
                          }).FirstOrDefault();

                // Parse the track list
                var tracks = from u in x.Element("lfm").Element("album").Element("tracks").Descendants("track")
                             select new lfm.Track
                             {
                                 Name = u.SafeGetElement("name").SafeGetStringValue(),
                                 AlbumPosition = u.SafeGetAttribute("rank").SafeGetInt32Value(),
                                 Duration = u.SafeGetElement("duration").SafeGetInt64Value(),
                                 URL = u.SafeGetElement("url").SafeGetUriValue(),
                                 Streamable = Converters.ToBool(u.SafeGetElement("streamable").SafeGetStringValue()),
                                 FullTrack = Converters.ToBool(u.SafeGetElement("streamable").SafeGetAttribute("fulltrack").SafeGetStringValue()),
                                 Artist = new lfm.Artist
                                 {
                                     Name = u.SafeGetElement("artist").SafeGetElement("name").SafeGetStringValue(),
                                     Url = u.SafeGetElement("artist").SafeGetElement("url").SafeGetUriValue()
                                 },
                             };

                foreach (var t in tracks) result.Tracks.Add(t);
            });

            // Parse the tag list
            var tags = await Generic.getTags(e);
            foreach (var t in tags) result.TopTags.Add(t);

            // return the full album
            return result;
        }

        public static async Task<List<lfm.Album>> search(String e)
        {
            List<lfm.Album> results = new List<lfm.Album>();

            // Make Method Run Asynchronously
            await Task.Run(() =>
            {
                XDocument x = XDocument.Parse(e);

                // Parse main album details
                var result = from u in x.Descendants("album")
                             select new lfm.Album
                             {
                                 Name = u.SafeGetElement("name").SafeGetStringValue(),
                                 Artist = new lfm.Artist { Name = u.SafeGetElement("artist").SafeGetStringValue() },
                                 ID = u.SafeGetElement("id").SafeGetInt64Value(),
                                 Streamable = Converters.ToBool(u.SafeGetElement("streamable").SafeGetStringValue()),
                                 ImageSmall = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "small").FirstOrDefault().SafeGetUriValue(),
                                 ImageMedium = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "medium").FirstOrDefault().SafeGetUriValue(),
                                 ImageLarge = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "large").FirstOrDefault().SafeGetUriValue(),
                                 ImageExtraLarge = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "extralarge").FirstOrDefault().SafeGetUriValue(),
                                 ImageMega = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "mega").FirstOrDefault().SafeGetUriValue(),
                                 URL = u.SafeGetElement("url").SafeGetUriValue()
                             };

                foreach (lfm.Album a in result) results.Add(a);
            });

            // return the full album
            return results;
        }
    }
}
